<?php
require_once eCMS_DIR . '/module/user/PhotoLibrary/model/PhotoLibraryModel.class.php';

class PhotoLibraryLibrary extends FileManagerLibrary {
	
	private $fkInstance;
	
	public function __construct($fkInstance) {
		$this->fkInstance = $fkInstance; 
		
		$list = new ModuleInstanceList();
		$list->getByPkModuleInstance($fkInstance);
		
		$this->setName($list->At(0)->name);
		
	}
	
	/**
	 * @param $dir
	 */
	public function getSubTree($dir) {
		
		$level = $this->getLevel($dir);
		$tree = array();

		if($level==0) {

			$list = new PhotoLibraryList();
			$list->GetAllItemsTitleOrdered();
			
			foreach ($list as $l) {
				if($l->canRead()); {
					array_push($tree, array('name'=>$l->title, 'type' => 'directory'));
				}
			}
		}
		
		return $tree;
		
	}

	/**
	 * @param $path
	 */
	public function getFolder($path) {
		
		$level = $this->getLevel($path);
		$r = array();

		if($level==0) {
			// Load PhotoLibraries from Instance
			
			$list = new PhotoLibraryList();
			$list->GetAllItemsTitleOrdered();
			
			foreach ($list as $l) {
				if($l->canRead()); {
					$createTime = null;
					$changeTime = null;
					$filesize = "";
					$info = array('Path'=> utf8_encode($path . $l->title),
								  'Filename'=>utf8_encode($l->title),
								  'File Type'=> 'dir',
								  'File Type Title' => 'Directory',
								  'Preview'=>$this->getIconUrl("dir_photo"),
								  'Properties'=>array(
									  'Date Created'=>$createTime!=null?date('d M Y H:i', $createTime):null, 
									  'Date Modified'=>$changeTime!=null?date('d M Y H:i', $changeTime):null, 
									  'Height'=>null,
									  'Width'=>null,
									  'Size'=>$filesize
								  ),
								  'Error'=>"",
								  'Code'=>0);
									
					$r[utf8_encode($l->title)] = $info;
				}
			}
		}
		elseif($level==1) {
			
			$libraryTitle = $this->getLastFolder($path);
			//echo("libname: " . $libraryTitle . "\n\n");
			
			//Get PhotoLibrary by Name
			
			$libList = new PhotoLibraryList();
			$libList->getByTitle($libraryTitle);

			$lib = $libList->At(0);
			if($lib->canRead()) {
				
				$photoList = $libList->At(0)->photos;
				foreach ($photoList as $p) {
					
					$filepath = "/". $lib->pkPhotoLibrary . "/". $p->pkPhoto;
					$createTime = null;
					$changeTime = null;
					$filesize = "";
					$info = array('Path'=> $filepath,
								  'Filename'=>utf8_encode($p->title),
								  'File Type'=> 'jpg',
								  'File Type Title' => 'Image',
								  'Preview'=>$this->getIconUrl("jpg"),
								  'ThumbUrl' => (file_exists($p->getFilePath("edit"))?"&h=".md5_file($p->getFilePath("edit")):null),
								  'Properties'=>array(
									  'Date Created'=>$createTime!=null?date('d M Y H:i', $createTime):null, 
									  'Date Modified'=>$changeTime!=null?date('d M Y H:i', $changeTime):null, 
									  'Height'=>null,
									  'Width'=>null,
									  'Size'=>$filesize
								  ),
								  'Error'=>"",
								  'Code'=>0);
					$r[$filepath] = $info;
									
				}
				
			//print_r($photoList);
			}

			
			
			
			// Load Photos from Library
		}
		else {
			die("Level: $level");
		}
		
		return $r;
		
	}

	/**
	 * @param $path
	 */
	public function getInfo($path) {
		return false;
	}

	/**
	 * @param $path
	 * @param $name
	 */
	public function addFolder($path, $name) {
		return false;
	}

	/**
	 * @param $path
	 * @param $file
	 */
	public function addFile($path, $file) {
		return false;
	}

	/**
	 * @param $path
	 */
	public function downloadFile($path) {
		
		$ap = explode("/", $path);
		
		$pkPhotoLibrary = $ap[1];
		$pkPhoto = $ap[2];
		
		$photo = new PhotoLibraryPhotoItem((int)$pkPhoto);
		
		$photo->createPublicFile();
		$filepath = $photo->getFilePath("public");
		
		//echo $photo->title;
		$this->sendFile($filepath, $photo->title . ".jpg");
		
	}

	/**
	 * @param $path
	 * @param $new
	 */
	public function rename($path, $new) {
		return false;
	}

	/**
	 * @param $path
	 */
	public function delete($path) {
		return false;
	}

	
	public function getThumb($path) {
		
		$ap = explode("/", $path);
		
		$pkPhotoLibrary = $ap[1];
		$pkPhoto = $ap[2];
		
		$photo = new PhotoLibraryPhotoItem((int)$pkPhoto);
		
		$photo->createPublicFile();
		$filepath = $photo->getFilePath("edit");
		
		//echo $photo->title;
		$this->sendFile($filepath, $photo->title . ".jpg");
		
	}
	
	/**
	 * 
	 */
	public function serialize() {
		
		$data["fkInstance"] = $this->fkInstance;
		
		return serialize($data);
		
	}

	/**
	 * @param $serialized
	 */
	public function unserialize($serialized) {
		
		$data = unserialize($serialized);
		
		$this->fkInstance = $data['fkInstance'];
	}

}

?>